//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.client.statistics;

import tv.dyndns.kishibe.client.Service;
import tv.dyndns.kishibe.client.packet.PacketRatingDistribution;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.gchart.client.GChart;

public class PanelStatisticsRatingDistribution extends VerticalPanel {
	private boolean first = true;
	private final AsyncCallback<PacketRatingDistribution> callbackGetRatingDistribution = new AsyncCallback<PacketRatingDistribution>() {
		public void onSuccess(PacketRatingDistribution result) {
			display(result);
		}

		public void onFailure(Throwable caught) {
		}
	};

	private void display(PacketRatingDistribution ratingDistribution) {
		add(new RatingDistributionChart(ratingDistribution));
	}

	protected void onLoad() {
		super.onLoad();

		if (!first) {
			return;
		}
		first = false;

		Service.Util.getInstance().getRatingDistribution(callbackGetRatingDistribution);
	}

	public class RatingDistributionChart extends GChart {
		public RatingDistributionChart(PacketRatingDistribution ratingDistribution) {
			super(512, 384);
			final int[] distribution = ratingDistribution.distribution;
			final int min = ratingDistribution.min;
			final int max = ratingDistribution.max;

			setChartTitle("<b><big>レーティング分布</big></b>");
			setChartFootnotesLeftJustified(true);
			setChartFootnotes("個人レーティングの計算式は<a href='http://ja.wikipedia.org/wiki/%E3%82%A4%E3%83%AD%E3%83%AC%E3%83%BC%E3%83%86%E3%82%A3%E3%83%B3%E3%82%B0' target='_blank'>こちら</a><br>" + //
					"(画面上では平方根を取って表示しています)<br>" + //
					"最高レーティング…" + ratingDistribution.max + "<br>" + //
					"最低レーティング…" + ratingDistribution.min + "<br>");

			addCurve();
			getCurve().getSymbol().setSymbolType(SymbolType.VBAR_SOUTH);
			getCurve().getSymbol().setBackgroundColor("red");
			getCurve().getSymbol().setBorderColor("black");
			for (int x = 0; x < distribution.length; ++x) {
				getCurve().addPoint(x / (double) distribution.length * (max - min) + min, distribution[x]);
			}
			getXAxis().setAxisLabel("レーティング");
			getXAxis().setTickLabelFormat("#");
			getYAxis().setAxisLabel("人数");
			getYAxis().setTickLabelFormat("#");
			update();
		}
	}
}
